package com.hxf.web.action.role;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.hxf.common.GlobalMPURL;
import com.hxf.common.JsonUtils;
import com.hxf.common.PublicUtil; 
import com.hxf.dto.AdminUserDto;
import com.hxf.dto.MenuDto;
import com.hxf.dto.ReturnData;
import com.hxf.dto.RoleDto; 
import com.hxf.dto.RoleMenuDto; 
import com.hxf.exception.DaoLevelException;
import com.hxf.exception.ServiceException;
import com.hxf.web.action.WebActionBase;
import com.hxf.web.vo.RoleMenuVo;

/**
 * 角色管理Action
 * @author tengyicheng
 * @version [1.0, 2013/09/22]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Results({@Result(name = "success", location = "../common/success.jsp",params={"actionName","role"}), @Result(name = "error", location = "../common/error.jsp"), @Result(name = "msg", location = "../common/msg.jsp")})
public class RoleAction extends WebActionBase {
	private static final long serialVersionUID = 1L;
	
	private static final int USER_TYPE_ID_VALUE = 0;

	private List<RoleDto> roleList;
	private List<MenuDto> menuList;
	private List<RoleMenuVo> rmVoList;
	
	private Integer roleId;
	private String roleName;
	private String createUser;
	private String createTime;
	private String remark;
	private Integer status;
	
	private String menuIds;
	private String jsonString;
	
	
	/**
	 * 角色信息
	 * @return
	 * @throws DaoLevelException 
	 */
	public String roleList() throws DaoLevelException {
		try {
			//菜单权限验证
			if (!menuPrivilegeValidate(GlobalMPURL.MENU_PRIVILEGE_URL)) {
				return "msg";
			}
			RoleDto rd = new RoleDto();
			rd.setRoleName(roleName);
			//UserDto userDto = (UserDto)getSession().getAttribute("userDto");//获取当前登陆 userID
			AdminUserDto adminUserDto = (AdminUserDto)getSession().getAttribute("adminUserDto");
			rd.setRoleId(adminUserDto.getId());
			//ReturnData<RoleDto> rdData = super.serviceFacade.getRoleService().findByRole(rd, targetPage, pageSize);
			ReturnData<RoleDto> rdData = super.serviceFacade.getRoleService().findByRoleOrAdmin(rd, targetPage, pageSize);
			roleList = rdData.getResultlist();
			setRecordCount(rdData.getRecordcount());
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		}
		return "role-list";
	}
	
	/**
	 * 跳转角色页面
	 * @return
	 */
	public String roleAdd() {
		//菜单权限验证
		if (!menuPrivilegeValidate(GlobalMPURL.MENU_PRIVILEGE_URL)) {
			return "msg";
		}
		return "role-add";
	}
	
	/**
	 * 修改角色信息
	 * @return
	 */
	public String roleUpdate() {
		try {
			//菜单权限验证
			if (!menuPrivilegeValidate(GlobalMPURL.MENU_PRIVILEGE_URL)) {
				return "msg";
			}
			RoleDto role = (RoleDto) super.serviceFacade.getRoleService().get(RoleDto.class, roleId);
			this.setRoleValue(role);
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		}
		return "role-update";
	}
	
	/**
	 * 保存或者修改角色信息
	 * @return
	 */
	public String roleSaveOrUpdate() {
		try {
			//菜单权限验证
			if (!menuPrivilegeValidate(GlobalMPURL.MENU_PRIVILEGE_URL)) {
				return "msg";
			}
			if (PublicUtil.isNotNull(roleId) && USER_TYPE_ID_VALUE != roleId) {  
				RoleDto roleDto = (RoleDto)super.serviceFacade.getUserService().get(RoleDto.class, roleId);
				this.setCreateTime(roleDto.getCreateTime());
				this.setCreateUser(roleDto.getCreateUser());
				RoleDto role = new RoleDto();
				this.setRole(role);
				super.serviceFacade.getUserService().update(role);//修改角色
			} else { 
				this.setRoleId(super.serviceFacade.getRoleService().getMaxId("roleId", "RoleDto")+1);
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				this.setCreateTime(sdf.format(new Date()));
				this.setCreateUser("admin");
				RoleDto role = new RoleDto();
				this.setRole(role);
				super.serviceFacade.getUserService().save(role);
			}
			requestURL = "role!roleList.html";
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		}
		return SUCCESS;
	}

	 /**
	  * 删除角色信息
	  * @return
	  */
	public String roleDelete() {
		try {
			//菜单权限验证
			if (!menuPrivilegeValidate(GlobalMPURL.MENU_PRIVILEGE_URL)) {
				return "msg";
			}
			RoleDto role = new RoleDto();
			role.setRoleId(roleId);
			super.serviceFacade.getUserService().delete(role);
			super.println("success");
		} catch (ServiceException e) { 
			logger.debug(e.getMessage(), e);
		} catch (IOException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	/**
	 * 跳转菜单权限页面
	 * @return
	 */
	public String roleAddPrivilege() {
		RoleDto roleDto;
		try {
			//菜单权限验证
			if (!menuPrivilegeValidate(GlobalMPURL.MENU_PRIVILEGE_URL)) {
				return "msg";
			}
			roleDto = (RoleDto)super.serviceFacade.getUserService().get(RoleDto.class, roleId);
			this.setRoleValue(roleDto);
			RoleMenuDto rmDto = new RoleMenuDto();
			rmDto.setId(roleDto.getRoleId());
			rmDto.setRoleId(roleDto.getRoleId());
			//查询绑定的菜单
			rmVoList = super.serviceFacade.getRoleService().findByRoleAddMenu(rmDto);
			jsonString = JsonUtils.listToJson(rmVoList); 
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		}
		
		return "role-add-menu";
	}
	
	/**
	 * 添加菜单权限
	 * @return
	 */
	public String roleAddPrivilegeSave() {
		try {
			//菜单权限验证
			if (!menuPrivilegeValidate(GlobalMPURL.MENU_PRIVILEGE_URL)) {
				return "msg";
			}
			RoleDto roleDto = new RoleDto();
			this.setRole(roleDto);
			super.serviceFacade.getRoleService().saveRoleAddMenu(roleDto, menuIds);
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		}
		requestURL = "role!roleList.html";
		return SUCCESS;
	}
	
	private void setRole(RoleDto roleDto) {
		roleDto.setCreateTime(this.getCreateTime());
		roleDto.setCreateUser(this.getCreateUser());
		roleDto.setStatus(this.getStatus());
		roleDto.setRoleId(this.getRoleId());
		roleDto.setRoleName(this.getRoleName());
		roleDto.setRemark(this.getRemark());
	}
	
	private void setRoleValue(RoleDto roleDto) {
		this.createTime = roleDto.getCreateTime();
		this.createUser = roleDto.getCreateUser();
		this.status = roleDto.getStatus();
		this.roleId = roleDto.getRoleId();
		this.roleName = roleDto.getRoleName();
		this.remark = roleDto.getRemark(); 
	}

	public List<RoleDto> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<RoleDto> roleList) {
		this.roleList = roleList;
	}

	public Integer getRoleId() {
		return roleId;
	}

	public void setRoleId(Integer roleId) {
		this.roleId = roleId;
	}

	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	public String getCreateUser() {
		return createUser;
	}

	public void setCreateUser(String createUser) {
		this.createUser = createUser;
	}

	public String getCreateTime() {
		return createTime;
	}

	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public List<MenuDto> getMenuList() {
		return menuList;
	}

	public void setMenuList(List<MenuDto> menuList) {
		this.menuList = menuList;
	}

	public List<RoleMenuVo> getRmVoList() {
		return rmVoList;
	}

	public void setRmVoList(List<RoleMenuVo> rmVoList) {
		this.rmVoList = rmVoList;
	}

	public String getMenuIds() {
		return menuIds;
	}

	public void setMenuIds(String menuIds) {
		this.menuIds = menuIds;
	}

	public String getJsonString() {
		return jsonString;
	}

	public void setJsonString(String jsonString) {
		this.jsonString = jsonString;
	}
	
	 
}
